﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.Mvc;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using FILSTARTUPS_MOBILE.Models;
using FILSTARTUPS_MOBILE.ViewModels;
using FILSTARTUPS_MOBILE.Helpers;
using System.IO;

namespace FILSTARTUPS_MOBILE.Controllers
{
    [Authorize]
    public class ProfileController : Controller
    {

        FilStatUpsEntities mobile_connect = new FilStatUpsEntities();
       
        // GET: /Profile/

        //Profile of Current User
        // int id?
        public ActionResult Index()
        {
            var user_email = mobile_connect.Members.Where(m => m.emailaddress.Equals(User.Identity.Name)).SingleOrDefault();
            var Profile = new ProfilePage();
            Profile.profile = mobile_connect.Profiles.Where(p=>p.memberidFK.Equals(user_email.memberidPK)).SingleOrDefault();
            Profile.PageNav = FilStartUps.GetPageNav();

            int age = Mobile_Helpers.CalculateAge(Profile.profile.birthdate);

            ViewBag.Age = age;

            return View(Profile);
        }

 
        public ActionResult EditProfile(int id)
        {
            try
            {
                var profile = new ProfilePage();
                profile.PageNav = FilStartUps.GetPageNav();
                profile.profile = mobile_connect.Profiles.Where(p => p.profileidPK.Equals(id)).SingleOrDefault();

                if (profile != null)
                {
                    return View(profile);
                }
                else
                {
                    //ERROR MESSAGE
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        //Validation
  
        [HttpPost, ValidateInput(false)]
        public ActionResult EditProfile(Profile profile,
                      FormCollection form, HttpPostedFileBase postedFile
                  
                      )
        {
            var user_ = mobile_connect.Members.Where(m => m.emailaddress.Contains(User.Identity.Name)).SingleOrDefault();
            try
            {
              
                if (ModelState.IsValid)
                {
                    profile.memberidFK = user_.memberidPK;
                
                    if (postedFile != null)
                    {
                        profile.filename = postedFile.FileName;
                        profile.imagetype = postedFile.ContentType;
                        int length = postedFile.ContentLength;
                        byte[] buffer = new byte[length];
                        postedFile.InputStream.Read(buffer, 0, length);
                        profile.image = buffer;
                    }
                    mobile_connect.Entry(profile).State = EntityState.Modified;
                    mobile_connect.SaveChanges();
                    return RedirectToAction("Index");
                }
                return null;
            }
            catch
            {
                return View();
            }
            
        }

        public ActionResult ProfilePicture(int id)
        {
            try
            {
                var profilepic= mobile_connect.Profiles.Where(p=>p.profileidPK.Equals(id)).SingleOrDefault();
                byte[] image = profilepic.image;
                return File(image, profilepic.imagetype);
            }
            catch
            {
                return null;
            }
            
        }


        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(int id,
                Member member,FormCollection form)
        {
            var user_ = mobile_connect.Members.Where(m=>m.emailaddress.Equals(User.Identity.Name)).SingleOrDefault();
            try
            {
                string current = form["password"].Trim();
                string newpassword = form["newpassword"].Trim();
                string retype = form["retypenewpassword"].Trim();

                var changepassword = mobile_connect.Members.Where(m => m.emailaddress.Equals(user_.emailaddress)
                                                                    && m.password.Equals(current)).SingleOrDefault();

                if (changepassword != null)
                {
                    changepassword.password = newpassword;
                    changepassword.confirmpassword = retype;
                    mobile_connect.Entry(changepassword).State = EntityState.Modified;
                    mobile_connect.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    //wrong password
                    return View();
                }
            
            }
            catch
            {
                return null;
            }
        }


        [ModeratorAccess]
        public ActionResult Settings()
        {
            // ViewBag Count Number of Reports
            return View();
        }

        //Other User
        public ActionResult ViewProfile(int id)
        {
            /* Check User Credentials Other Users
             * Member or Guest 
             * */
            if (User.Identity.IsAuthenticated)
            {
                return View();
            }
            else
            {
                //TODO --> ERROR MSG:
                return null;
            }
        }
      
    }
}
